package org.grayrabbit.cms.util.lucene;

import java.io.IOException;
import java.io.StringReader;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;

public class WordUtil {
	/**
	 * 拼音
	 * 
	 * @param word
	 * @return
	 */
	public static String getPinyin(String word) {
		StringBuffer pybf = new StringBuffer();
		char[] arr = word.toCharArray();
		HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
		defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		for (int i = 0; i < arr.length; i++) {
			if (arr[i] > 128) {
				try {
					if (Character.toString(arr[i]).matches("[\\u4E00-\\u9FA5]+")){
						String py = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat)[0];
						pybf.append(py.substring(0,1).toUpperCase()+py.substring(1));
					}else{
						pybf.append(arr[i]);
					}
					
				} catch (BadHanyuPinyinOutputFormatCombination e) {
					e.printStackTrace();
				}
			} else {
				pybf.append(arr[i]);
			}
		}
		return pybf.toString();
	}

	/**
	 * 首字母
	 * 
	 * @param word
	 * @return
	 */
	public static String getFirstLetter(String word) {
		StringBuffer pybf = new StringBuffer();
		char[] arr = word.toCharArray();
		HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
		defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		for (int i = 0; i < arr.length; i++) {
			if (arr[i] > 128) {
				try {
					String[] _t = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat);
					if (_t != null) {
						pybf.append(_t[0].charAt(0));
					}
				} catch (BadHanyuPinyinOutputFormatCombination e) {
					e.printStackTrace();
				}
			} else {
				pybf.append(arr[i]);
			}
		}
		return pybf.toString().replaceAll("\\W", "").trim().toUpperCase();
	}

	/**
	 * 中文分词结果
	 * @param word
	 * @param splitchar
	 * @param addword 是否返回单个字
	 * @return
	 */
	public static String getSplitWord(String word,String splitchar,boolean addword) {
		TokenStream tokeStream = AnalyzerUtil.getIkAnalyzer().tokenStream("", new StringReader(word));
		tokeStream.addAttribute(CharTermAttribute.class);
		StringBuffer sb = new StringBuffer();
		try {
			while (tokeStream.incrementToken()) {
				CharTermAttribute ta = tokeStream.getAttribute(CharTermAttribute.class);
				if(!addword&&ta.length()==1){
					continue;
				}
				else{
					sb.append(ta.toString());
					sb.append(splitchar);
				}
				
			}
			if(sb.length()>0){
				sb.delete(sb.length()-splitchar.length(), sb.length());
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		finally{
			try {
				tokeStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();

	}	
	public static String getUniqueSplitWord(String word,String splitchar,boolean addword) {
		TokenStream tokeStream = AnalyzerUtil.getIkAnalyzer().tokenStream("", new StringReader(word));
		tokeStream.addAttribute(CharTermAttribute.class);
		Set<String> set = new HashSet<String>();
		try {
			while (tokeStream.incrementToken()) {
				CharTermAttribute ta = tokeStream.getAttribute(CharTermAttribute.class);
				if(!addword&&ta.length()==1){
					continue;
				}
				else{
					set.add(ta.toString());
				}
				
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		finally{
			try {
				tokeStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		StringBuffer sb = new StringBuffer();
		Iterator<String> it = set.iterator();
		while(it.hasNext()){
			sb.append(it.next());
			sb.append(splitchar);
			
		}
		return sb.delete(sb.length()-splitchar.length(), sb.length()).toString();
	}
}
